package com.tskj.leonsu.dao;

import com.alibaba.fastjson.JSONArray;
import com.tskj.leonsu.db.DbUtilitys;
import com.tskj.leonsu.utility.ModuleType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class BadmintonDAO implements OrderBiz {

    private final String MODULE_TYPE = String.valueOf(ModuleType.YU_MAO_QIU.ordinal());

    @Autowired
    private DbUtilitys dbUtilitys;

    @Override
    public JSONArray getUnFinishedOrder(String adUserName) {
        String sql = "select orderid id, " + MODULE_TYPE + " type, bname address, startdate date, concat(substr(starttime, 1, 5),' - ', substr(endtime, 1, 5)) time, qrcode code, orderstate status, xueyuan, date_format(createdate, '%Y-%m-%d %H:%i:%S') createdate, " +
                "date_sub(str_to_date(concat(startdate, starttime),'%Y-%m-%d%T'), interval 30 minute) > now() canCancel, " +
                "str_to_date(concat(startdate, endtime),'%Y-%m-%d%T') < now() canConfirm, pcount count " +
                "from badminton_order " +
                "where createdate>=DATE_SUB(date(now()), INTERVAL 5 day) and adusername=? and orderstate=0 " +
                "order by createdate desc";
        List<Map<String, Object>> list = dbUtilitys.execSQL(sql, adUserName);
        return JSONArray.parseArray(JSONArray.toJSONString(list));
    }

    @Override
    public JSONArray getFinishedOrder(String adUserName) {
        String sql = "select orderid id, " + MODULE_TYPE + " type, bname address, startdate date, concat(substr(starttime, 1, 5),' - ', substr(endtime, 1, 5)) time, qrcode code, orderstate status, xueyuan, date_format(createdate, '%Y-%m-%d %H:%i:%S') createdate, pcount count from badminton_order " +
                "where createdate>=DATE_SUB(date(now()), INTERVAL 5 day) and adusername=? and orderstate=1 " +
                "order by createdate desc";
        List<Map<String, Object>> list = dbUtilitys.execSQL(sql, adUserName);
        return JSONArray.parseArray(JSONArray.toJSONString(list));
    }

    @Override
    public JSONArray getAllOrder(String adUserName) {
        String sql = "select orderid id, " + MODULE_TYPE + " type, bname address, startdate date, concat(substr(starttime, 1, 5),' - ', substr(endtime, 1, 5)) time, qrcode code, orderstate status, xueyuan, date_format(createdate, '%Y-%m-%d %H:%i:%S') createdate, pcount count from badminton_order " +
                "where createdate>=DATE_SUB(date(now()), INTERVAL 5 day) and adusername=? " +
                "order by createdate desc";
        List<Map<String, Object>> list = dbUtilitys.execSQL(sql, adUserName);
        return JSONArray.parseArray(JSONArray.toJSONString(list));
    }

    @Override
    public Boolean getCanConfirm(String orderId) {
        String sql = "select str_to_date(concat(startdate, endtime),'%Y-%m-%d%T') < now() canConfirm from badminton_order where orderid=?";
        List<Map<String, Object>> list = dbUtilitys.execSQL(sql, orderId);
        if (list == null || list.size() == 0) {
            return false;
        }
        Object value = list.get(0).get("canConfirm");
        if (value == null || !"1".equals(value.toString())) {
            return false;
        }
        return true;
    }

    @Override
    public void confirmOrder(String adUserName, String orderId) throws Exception {
        String sql = "update badminton_order set orderstate=1, ctime=now() where orderid=? and adusername=? and str_to_date(concat(startdate, endtime),'%Y-%m-%d%T') < now()";
        dbUtilitys.execSQLWithTran(sql, orderId, adUserName);
    }

    @Override
    public boolean getCanCancel(String orderId) {
        String sql = "select date_sub(str_to_date(concat(startdate, starttime),'%Y-%m-%d%T'), interval 30 minute) > now() canCancel from badminton_order where orderid=?";
        List<Map<String, Object>> list = dbUtilitys.execSQL(sql, orderId);
        if (list == null || list.size() == 0) {
            return false;
        }
        Object value = list.get(0).get("canCancel");
        if (value == null || !"1".equals(value.toString())) {
            return false;
        }
        return true;
    }

    @Override
    public void cancelOrder(String adUserName, String orderId) throws Exception {
        String sql = "update badminton_order set orderstate=2, ctime=now() where orderid=? and adusername=? and date_sub(str_to_date(concat(startdate, starttime),'%Y-%m-%d%T'), interval 30 minute) > now()";
        dbUtilitys.execSQLWithTran(sql, orderId, adUserName);
    }
}
